package com.courseSchedulingSystem.controller.student;


import com.courseSchedulingSystem.common.result.Result;
import com.courseSchedulingSystem.controller.base.BaseController;
import com.courseSchedulingSystem.pojo.dto.StudentLoginDTO;
import com.courseSchedulingSystem.pojo.vo.StudentLoginVO;
import com.courseSchedulingSystem.service.impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/student")
public class StudentController extends BaseController {
    StudentLoginVO studentLoginVO;
    @Autowired
    private StudentServiceImpl studentService;

    @PostMapping("/login")
    public Result<StudentLoginVO> login(@RequestBody Map<String, String> data) {
        int stuId = Integer.parseInt(data.get("stuId"));
        String stuPassword = data.get("stuPassword");
        StudentLoginDTO studentLoginDTO = new StudentLoginDTO(stuId,stuPassword);
        System.out.println(studentLoginDTO);
        log.info("用户登录请求{}",studentLoginDTO);
        studentLoginVO = studentService.login(studentLoginDTO);
        if (studentLoginVO == null) {
            return Result.error("账号或者密码错误");
        }
        studentLoginVO.setStuId(studentLoginDTO.getStuId());
        return Result.success(studentLoginVO);
    }

    @PostMapping("/update/password")
    public Result<Boolean> updatePassword(@RequestBody Map<String, String> data) {
        String ordStuPassword = data.get("ordStuPassword");
        String newStuPassword = data.get("newStuPassword");

        log.info("用户修改密码请求{}{}",ordStuPassword,newStuPassword);
        Boolean aBoolean = studentService.upDatePassword(ordStuPassword,newStuPassword);
        return Result.success(aBoolean);
    }

    @GetMapping("/getCurrentSemester")
    public Result<Map<String, String>> getCurrentSemester() {
        log.info("获取当前学期信息");
        Map<String, String> semester = studentService.getCurrentSemester();
        return Result.success(semester);
    }


    @PostMapping("/coursesSelect")
    public Result<String> selectCourse(@RequestParam String userId, @RequestParam String courseId) {
        log.info("用户 {} 选课: {}", userId, courseId);
        String message = studentService.selectCourse(userId, courseId);
        return Result.success(message);
    }


    @PostMapping("/coursesDrop")
    public Result<String> dropCourse(@RequestParam String userId, @RequestParam String courseId) {
        log.info("用户 {} 退课: {}", userId, courseId);
        String message = studentService.dropCourse(userId, courseId);
        return Result.success(message);
    }

}
